package com.mes.cloud.service.productpricegroup;

import com.mes.cloud.api.dto.request.productpricegroup.ProductCustomerGroupCustomerBatchDeleteRequest;
import com.mes.cloud.api.dto.request.productpricegroup.ProductCustomerGroupCustomerBindCustomerRequest;
import com.mes.cloud.api.dto.request.productpricegroup.ProductCustomerGroupCustomerListRequest;
import com.mes.cloud.api.dto.response.productpricegroup.ProductCustomerGroupCustomerDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.ProductCustomerGroupCustomerDao;
import com.mes.cloud.entity.ProductCustomerGroupCustomer;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.IPKeyGenerator;
import com.mes.cloud.util.CommonUtil;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 产品客户组-客户关联
 *
 * @author wxm
 * @version 2019-09-24 14:49:06
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class ProductCustomerGroupCustomerService {

    private ProductCustomerGroupCustomerDao productCustomerGroupCustomerDao;

    private IPKeyGenerator iPKeyGenerator;

    public ProductCustomerGroupCustomerService(ProductCustomerGroupCustomerDao productCustomerGroupCustomerDao, IPKeyGenerator iPKeyGenerator) {
        this.productCustomerGroupCustomerDao = productCustomerGroupCustomerDao;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 查列表
     *
     * @param productCustomerGroupCustomerListRequest
     * @return JsonResult
     */
    public JsonResult<List<ProductCustomerGroupCustomerDto>> list(ProductCustomerGroupCustomerListRequest productCustomerGroupCustomerListRequest) {
        //查询条件
        ProductCustomerGroupCustomer selectParam = new ProductCustomerGroupCustomer();
        BeanUtils.copyProperties(productCustomerGroupCustomerListRequest, selectParam);
        selectParam.setDelFlag(Constants.DEL_FLAG_NO);
        List<ProductCustomerGroupCustomer> productCustomerGroupCustomerList = productCustomerGroupCustomerDao.select(selectParam);
        List<ProductCustomerGroupCustomerDto> productCustomerGroupCustomerDtoList = new ArrayList<>();
        for (ProductCustomerGroupCustomer productCustomerGroupCustomer : productCustomerGroupCustomerList) {
            ProductCustomerGroupCustomerDto productCustomerGroupCustomerDto = new ProductCustomerGroupCustomerDto();
            BeanUtils.copyProperties(productCustomerGroupCustomer, productCustomerGroupCustomerDto);
            productCustomerGroupCustomerDtoList.add(productCustomerGroupCustomerDto);
        }
        JsonResult<List<ProductCustomerGroupCustomerDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productCustomerGroupCustomerDtoList);
        return jsonResult;
    }

    /**
     * 绑定客户
     * 更新客户组的客户数
     * 更新价格组客户数
     *
     * @param productCustomerGroupCustomerBindCustomerRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult bindCustomer(ProductCustomerGroupCustomerBindCustomerRequest productCustomerGroupCustomerBindCustomerRequest) {
        JsonResult jsonResult = new JsonResult();
        String productCustomerGroupId = productCustomerGroupCustomerBindCustomerRequest.getProductCustomerGroupId();
        //获取获取客户组已有客户
        ProductCustomerGroupCustomer productCustomerGroupCustomerParam = new ProductCustomerGroupCustomer();
        productCustomerGroupCustomerParam.setProductCustomerGroupId(productCustomerGroupId);
        //未删除状态
        productCustomerGroupCustomerParam.setDelFlag(Constants.DEL_FLAG_NO);
        List<ProductCustomerGroupCustomer> existProductCustomerGroupCustomerList = productCustomerGroupCustomerDao.select(productCustomerGroupCustomerParam);

        //新增客户剔除已有客户
        //客户组已有客户列表     前端传递的客户列表
        //两个列表做比对，已有客户不需要新增
        Set<String> idList = deleteExistCustomerId(existProductCustomerGroupCustomerList, productCustomerGroupCustomerBindCustomerRequest.getIds());
        List<ProductCustomerGroupCustomer> productCustomerGroupCustomerList = generatorProductCustomerGroupCustomer(idList, productCustomerGroupCustomerBindCustomerRequest.getProductCustomerGroupId());
        if (CollectionUtils.isEmpty(productCustomerGroupCustomerList)) {
            return jsonResult.setCode(HttpStatusEnum.REF_EXIST.getCode()).setMsg(HttpStatusEnum.REF_EXIST.getMessage());
        }
        //批量插入
        int count = productCustomerGroupCustomerDao.batchInsert(productCustomerGroupCustomerList);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 封装关联表
     *
     * @param idList
     * @param productCustomerGroupId
     * @return
     */
    private List<ProductCustomerGroupCustomer> generatorProductCustomerGroupCustomer(Set<String> idList, String productCustomerGroupId) {
        List<ProductCustomerGroupCustomer> productCustomerGroupCustomerList = new ArrayList<ProductCustomerGroupCustomer>();
        if (CollectionUtils.isEmpty(idList)) {
            return productCustomerGroupCustomerList;
        }
        ProductCustomerGroupCustomer productCustomerGroupCustomerTemp = null;
        for (String id : idList) {
            productCustomerGroupCustomerTemp = generatorProductCustomerGroupCustomer(productCustomerGroupId, id);
            productCustomerGroupCustomerList.add(productCustomerGroupCustomerTemp);
        }
        return productCustomerGroupCustomerList;
    }

    /**
     * 封装一个ProductCustomerGroupCustomer
     *
     * @param productCustomerGroupId
     * @param customerId
     * @return
     */
    private ProductCustomerGroupCustomer generatorProductCustomerGroupCustomer(String productCustomerGroupId, String customerId) {
        ProductCustomerGroupCustomer productCustomerGroupCustomerTemp = new ProductCustomerGroupCustomer();
        productCustomerGroupCustomerTemp.setId(iPKeyGenerator.getGenerateKey());
        productCustomerGroupCustomerTemp.setProductCustomerGroupId(productCustomerGroupId);
        productCustomerGroupCustomerTemp.setCustomerId(customerId);
        productCustomerGroupCustomerTemp.setUpdateDateStr(DateUtil.updateDateStr());
        productCustomerGroupCustomerTemp.setCreateBy(UserUtil.getCreateByName());
        productCustomerGroupCustomerTemp.setUpdateBy(UserUtil.getUpdateByName());
        productCustomerGroupCustomerTemp.setCreateByNo(UserUtil.getCreateByNo());
        productCustomerGroupCustomerTemp.setUpdateByNo(UserUtil.getUpdateByNo());
        return productCustomerGroupCustomerTemp;
    }

    /**
     * 两个客户列表做对比，客户列表中已经存在的ID剔除
     *
     * @param existProductCustomerGroupCustomerList 数据库中又有的客户列表
     * @param ids                                   前端传递的客户列表
     * @return
     */
    private Set<String> deleteExistCustomerId(List<ProductCustomerGroupCustomer> existProductCustomerGroupCustomerList, Set<String> ids) {
        if (CollectionUtils.isEmpty(existProductCustomerGroupCustomerList)) {
            return ids;
        }
        Set<String> existIdList = new HashSet<>();
        //员工已经拥有的角色ID存放到existIdList
        for (ProductCustomerGroupCustomer productCustomerGroupCustomer : existProductCustomerGroupCustomerList) {
            existIdList.add(productCustomerGroupCustomer.getCustomerId());
        }
        return CommonUtil.deleteExistId(existIdList, ids);
    }

    /**
     * 解绑客户
     *
     * @param productCustomerGroupCustomerBatchDeleteRequest
     * @return
     */
    public JsonResult batchDelete(ProductCustomerGroupCustomerBatchDeleteRequest productCustomerGroupCustomerBatchDeleteRequest) {
        JsonResult jsonResult = new JsonResult();
        productCustomerGroupCustomerDao.batchDelete(productCustomerGroupCustomerBatchDeleteRequest.getIds(), DateUtil.updateDateStr(),
                UserUtil.getEmployeeNo(),UserUtil.getName());
        return jsonResult;
    }

}
